package com.funo.main.ctrler;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.funo.common.factory.JsonMsgFactory;
import com.funo.common.pojo.PageResult;
import com.funo.common.pojo.QueryCondition;
import com.funo.common.pojo.TreeNode;
import com.funo.common.utils.Constant;
import com.funo.core.base.BaseCtrler;
import com.funo.main.entities.Role;
import com.funo.main.entities.RoleModule;
import com.funo.main.service.ModuleService;
import com.funo.main.service.RoleService;

/**
 * 角色模块业务逻辑处理实现类
 * 
 * @author wengx,zhengjs
 *
 */
@Controller
@RequestMapping("/role")
public class RoleCtrler extends BaseCtrler {
	@Autowired
	private RoleService roleService;//角色service
	
	@Autowired
	private ModuleService moduleService;//模块service

	/**
	 * 查询所有角色信息
	 * 
	 * @param condition 查询条件集合
	 * @return
	 */
	@RequestMapping(value = "/page")
	public @ResponseBody String page(QueryCondition condition) {
		PageResult<Role> page = roleService.getRolePage(condition);
		return toJson(page);
	}

	/**
	 * 新增角色操作
	 * 
	 * @param session session会话
	 * @param role 角色表
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody String add(HttpSession session,Role role) {
		
		if(sessionUser(session) == null || sessionUser(session).getId() == null){//丢失session
			return toJson(JsonMsgFactory.fail("请先登录,再进行操作!"));
		}
		
		if (role != null) {//角色不为空
			
			try {				
				
				Integer type=roleService.addRole(sessionUser(session),role);
				
				if (type == Constant.STATUS_SUSSESS) {//成功
					return toJson(JsonMsgFactory.successMsg());
				}
				
				if (type == Constant.STATUS_REPEAT) {//重复添加
					return toJson(JsonMsgFactory.fail("请勿重复添加,添加失败"));
				}
			} catch (Exception e) {//失败
				e.printStackTrace();
				return toJson(JsonMsgFactory.failMsg());
			}
		}
		
		return null;
	}
	
	/**
	 * 读取角色信息
	 * 
	 * @param id
	 * 		  主键id
	 * @return 角色信息
	 */
	@RequestMapping(value = "/loadRole", method = RequestMethod.POST)
	public @ResponseBody String loadRole(String id) {
		Role role=roleService.getRole(id);
		return toJson(role);
	}
	
	/**
	 * 修改角色操作
	 * 
	 * @param session session会话
	 * @param role 角色表
	 * @return 
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public @ResponseBody String edit(HttpSession session,Role role){
		
		if(sessionUser(session) ==null || sessionUser(session).getId() == null){//丢失session
			return toJson(JsonMsgFactory.fail("请先登录,再进行操作!"));
		}
		
		try {

			Integer type = roleService.updateRole(sessionUser(session),role);//修改操作
			
			if (type == Constant.STATUS_SUSSESS) {
				return toJson(JsonMsgFactory.successMsg());
			}
			
			if (type == Constant.STATUS_REPEAT) {
				return toJson(JsonMsgFactory.fail("修改的信息已存在，修改失败"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return toJson(JsonMsgFactory.failMsg());
		}
		
		return null;
	}
	
	/**
	 * 
	 * 删除角色操作
	 * 
	 * @param deleteIds ID集合
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody String delete(String deleteIds){
		
		try {

			String[]Ids=deleteIds.split(",");//传递到数据库的参数

			for (int i = 0; i < Ids.length; i++) {
				String id=Ids[i];
				Role role=roleService.getRole(id);//角色信息
				roleService.deleteRole(role);//删除角色
			}

			return toJson(JsonMsgFactory.successMsg());
			
		} catch (Exception e) {
			return toJson(JsonMsgFactory.failMsg());
		}
	}
	
	/**
	 * 角色模块信息
	 * 
	 * @param id 角色id
	 * @return
	 */
	@RequestMapping(value = "/roleModule")
	public @ResponseBody String roleModule(String id) {
		TreeNode root = moduleService.roleModuleTree(id);
		return toJson(root);
	}
	
	/**
	 * 选中已经选择的模块功能
	 * 
	 * @param roleModule 角色模块表
	 * @param checked 是否已经选择,true已经选择,false未选择
	 * @return
	 */
	@RequestMapping(value = "/check")
	public @ResponseBody String check(RoleModule roleModule,@RequestParam(value="checked")boolean checked){
		try {
			moduleService.checkRoleModule(roleModule, checked);
			return toJson(JsonMsgFactory.successMsg());
		} catch (Exception e) {
			e.printStackTrace();
			return toJson(JsonMsgFactory.failMsg());
		}
	}
	
	@Override
	protected String handlerException(Exception e) {
		e.printStackTrace();
		System.err.println("[handlerException:MemberController]异常类型："+e.getClass().getSimpleName());
		System.err.println(e.getMessage());
		return "系统异常";
	}
}
